package com.baibei.ebec.user.auth;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baibei.basic.IPresenter;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.user.R;
import com.baibei.ebec.user.R2;
import com.baibei.model.UserAuthInfo;
import com.baibei.module.AppRouter;
import com.baibei.module.BasicActivity;
import com.baibei.sdk.ApiDefaultObserver;
import com.baibei.sdk.RxObservable;

import butterknife.BindView;

/**
 * 认证状态
 * Created by ChenRui on 2017/8/16 0016 17:00.
 */
@Route(path = AppRouter.ROUTER_NAME_AUTH_STATUS)
public class NameAuthStatusActivity extends BasicActivity {

    @BindView(R2.id.tv_id_card)
    TextView mIdCardView;
    @BindView(R2.id.tv_name)
    TextView mNameView;
    private String mIdCard;
    private String mName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_name_auth_status);

        mIdCard = getIntent().getStringExtra("idCard");
        mName = getIntent().getStringExtra("name");

        if (!TextUtils.isEmpty(mIdCard) && !TextUtils.isEmpty(mName)) {
            onLoadData(mIdCard, mName);
        } else {
            // 从接口获取
            RxObservable.create(ApiFactory.getInstance().getUserApi().getUserAuthInfo(), this.toString())
                    .subscribe(new ApiDefaultObserver<UserAuthInfo>() {
                        @Override
                        protected void onError(String s) {
                            String msg = "获取实名认证信息失败";
                            mNameView.setHint(msg);
                            mIdCardView.setHint(msg);
                        }

                        @Override
                        protected void accept(UserAuthInfo userAuthInfo) {
                            onLoadData(userAuthInfo.getCertNo(), userAuthInfo.getRealName());
                        }
                    });
        }
    }

    @Override
    protected void onDestroy() {
        RxObservable.dispose(this.toString());
        super.onDestroy();
    }

    private void onLoadData(String idCard, String name) {
        // 隐藏敏感信息
        mIdCardView.setText(blockIdCard(idCard));
        mNameView.setText(blockName(name));
    }

    private String blockName(String name) {
        if (TextUtils.isEmpty(name)) return name;
        char[] chars = name.toCharArray();
        int length = chars.length;
        for (int i = 0; i < length; i++) {
            if (i == 0 || i == length - 1) continue;
            chars[i] = '*';
        }
        return new String(chars);
    }

    private String blockIdCard(String idCard) {
        if (TextUtils.isEmpty(idCard)) return idCard;
        char[] chars = idCard.toCharArray();
        int length = chars.length;
        for (int i = 0; i < length; i++) {
            if (i < 4 || i > length - 4) continue;
            chars[i] = '*';
        }
        return new String(chars);
    }


    @Override
    public IPresenter getBasicPresenter() {
        return null;
    }
}
